
/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) :
        val(x), next(NULL) {
    }
};
*/
class Solution {
public:
    ListNode* EntryNodeOfLoop(ListNode* pHead) {
        //第一步确定是否存在环
        ListNode* fast = pHead;
        ListNode* slow = pHead;
        while(fast->next)
        {
            fast = fast->next->next;
            slow = slow->next;
            if(fast == slow)
            {
                //存在环从起始节点开始往后移动
                ListNode* cur = pHead;
                while(cur!=fast)
                {
                    cur = cur->next;
                    fast = fast->next;
                }
                return cur;
            }
            if(fast == nullptr)
            {
                //不存在环
                return nullptr;
            }

        }
        return nullptr;
    }
};